package com.gusha.service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysTicketTypeDao;

import com.gusha.model.SysTicketType;

@Service
@Transactional
public class SysTicketTypeService {

	@Autowired
	private SysTicketTypeDao sysTicketTypeDao;
	
	public Page<SysTicketType> findAll(Pageable pageable, SysTicketType sysTicketType) {
	    @SuppressWarnings("serial")
	    Page<SysTicketType> result = sysTicketTypeDao.findAll(new Specification<SysTicketType>() {
	        @Override
	        public Predicate toPredicate(Root<SysTicketType> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
	            List<Predicate> list = new ArrayList<Predicate>();
	            if (sysTicketType.getId() != null) {
	                list.add(cb.equal(root.get("id").as(BigInteger.class),sysTicketType.getId()));
	            }
	            if (sysTicketType.getTicketType() != null) {
	                list.add(cb.equal(root.get("ticketType").as(String.class),sysTicketType.getTicketType()));
	            }
	            if (sysTicketType.getScenicName() != null) {
	                list.add(cb.equal(root.get("scenicName").as(String.class),sysTicketType.getScenicName()));
	            }
	            if (sysTicketType.getTicketInfo() != null) {
	                list.add(cb.equal(root.get("ticketInfo").as(String.class),sysTicketType.getTicketInfo()));
	            }
	            if (sysTicketType.getAddress() != null) {
	                list.add(cb.equal(root.get("address").as(String.class),sysTicketType.getAddress()));
	            }
	            if (sysTicketType.getCloseTime() != null) {
	                list.add(cb.equal(root.get("closeTime").as(Date.class),sysTicketType.getCloseTime()));
	            }
	            if (sysTicketType.getTicketPrice() != null) {
	                list.add(cb.equal(root.get("ticketPrice").as(BigDecimal.class),sysTicketType.getTicketPrice()));
	            }
	            if (sysTicketType.getTicketUnit() != null) {
	                list.add(cb.equal(root.get("ticketUnit").as(String.class),sysTicketType.getTicketUnit()));
	            }
	            if (sysTicketType.getContactNum() != null) {
	                list.add(cb.equal(root.get("contactNum").as(String.class),sysTicketType.getContactNum()));
	            }
	            if (sysTicketType.getAddTime() != null) {
	                list.add(cb.equal(root.get("addTime").as(Date.class),sysTicketType.getAddTime()));
	            }
	            if (sysTicketType.getAddName() != null) {
	                list.add(cb.equal(root.get("addName").as(String.class),sysTicketType.getAddName()));
	            }
	            if (sysTicketType.getUpdateTime() != null) {
	                list.add(cb.equal(root.get("updateTime").as(Date.class),sysTicketType.getUpdateTime()));
	            }
	            if (sysTicketType.getUpdateName() != null) {
	                list.add(cb.equal(root.get("updateName").as(String.class),sysTicketType.getUpdateName()));
	            }
	            if (sysTicketType.getOutTicket() != null) {
	                list.add(cb.equal(root.get("outTicket").as(String.class),sysTicketType.getOutTicket()));
	            }
	            if (sysTicketType.getTicketImg() != null) {
	                list.add(cb.equal(root.get("ticketImg").as(String.class),sysTicketType.getTicketImg()));
	            }
	            
	            if (sysTicketType.getIsDelete() != null) {
	                list.add(cb.equal(root.get("isDelete").as(String.class),sysTicketType.getIsDelete()));
	            }
	            Predicate[] p = new Predicate[list.size()];
	            return cb.and(list.toArray(p));
	        }
	    },pageable);

		return result;

	}

}
